﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.sprite.particle;

namespace Imaginecup.util
{
    public enum RenderMode { FADE_IN = 0, FADE_OUT };
    public class FadeHelper:IColorClass
    {  
        private RenderMode m_Mode = RenderMode.FADE_IN;
        private int m_AlphaValue = 0;
        private double m_CurrentTime = 0;
        private double m_DestDuration = 0;


        private bool m_bFadeOut;
        private bool m_bFadeIn;

        public void SetFadeMode(RenderMode mode)
        {
            if (mode == RenderMode.FADE_IN)
                m_AlphaValue = 255;
            else
                m_AlphaValue = 0;
            m_Mode = mode;
        }

        public void SetTime(int duration_ms)
        {
            m_CurrentTime = 0;
            m_DestDuration = duration_ms;
            m_bFadeOut = false;
            m_bFadeIn = false;
        }

        public Color GetColor()
        {
            return new Color(255, 255, 255, (Byte)m_AlphaValue);
        }

        public void Update(GameTime gameTime)
        {   
            // 몇 ms 지났는지 계산.
            m_CurrentTime += gameTime.ElapsedGameTime.TotalSeconds;

            

            if (m_Mode == RenderMode.FADE_IN)
            {
                m_AlphaValue = 255 - (int)((float)(m_CurrentTime / m_DestDuration) * 255.0f);

                // byte는 0~255까지 허용. 최소값 체크
                if (m_AlphaValue < 0)
                {
                    m_AlphaValue = 0;
                    m_bFadeIn = true;
                }
            }

            else if (m_Mode == RenderMode.FADE_OUT)
            {
                m_AlphaValue = (int)((float)(m_CurrentTime / m_DestDuration) * 255.0f);

                // byte는 0~255까지 허용. 최대값 체크
                if (m_AlphaValue > 255)
                {
                    m_AlphaValue = 255;
                    m_bFadeOut = true;
                }
            }
        }

        public bool CompleteRender()
        {
            if (m_Mode == RenderMode.FADE_OUT)
                return m_bFadeOut;

            else if (m_Mode == RenderMode.FADE_IN)
                return m_bFadeIn;
            return false;
        }

        private bool completeFadeOut()
        {
            return m_bFadeOut;
        }

        private bool completeFadeIn()
        {
            return m_bFadeIn;
        }
    }
}
